﻿using QuanLyChoVayCaNhan.Forms;
using QuanLyChoVayCaNhan.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyChoVayCaNhan.Classes
{
    public class NhanVienTab : BaseTab
    {
        public NhanVienTab()
        {

        }

        public NhanVienTab(System.Windows.Forms.BindingSource nhanVienBindingSource)
        {
            // TODO: Complete member initialization
            this.MainBindingDataSource = nhanVienBindingSource;
        }

        public override MenuItemType[] GetSupportedMenus()
        {
            return new MenuItemType[] { MenuItemType.ADD, MenuItemType.VIEW, MenuItemType.EDIT, MenuItemType.DELETE, MenuItemType.REFRESH };
        }

        public override void LoadData()
        {
            if (MainBindingDataSource != null) MainBindingDataSource.DataSource = ModelManager.GetInstance().db.NhanViens.ToList();
        }

        public override string GetTabTitle()
        {
            return "Nhân viên";
        }
        public override void OnAddClick()
        {
            AddNhanVien fAddNhanVien = new AddNhanVien();
            fAddNhanVien.FormClosed += fAddNhanVien_FormClosed;
            fAddNhanVien.ShowDialog();
        }
        public override void OnRefreshClick()
        {
            LoadData();
        }

        public override void OnViewClick()
        {
            if (WindowsManager.GetInstance().TabNhanVien.SelectedIndex == -1)
            {
                MessageBox.Show("Bạn chưa chọn nhân viên nào");
                return;
            }
            AddNhanVien fViewNhanVien = new AddNhanVien(MenuItemType.VIEW);
            fViewNhanVien.FormClosed += fAddNhanVien_FormClosed;
            fViewNhanVien.ShowDialog();
        }

        public override void OnEditClick()
        {
            if (WindowsManager.GetInstance().TabNhanVien.SelectedIndex == -1)
            {
                MessageBox.Show("Bạn chưa chọn nhân viên nào");
                return;
            }
            AddNhanVien fEditNhanVien = new AddNhanVien(MenuItemType.EDIT);
            fEditNhanVien.FormClosed += fAddNhanVien_FormClosed;
            fEditNhanVien.ShowDialog();
        }

        public override void OnDeleteClick()
        {
            if (WindowsManager.GetInstance().TabNhanVien.SelectedIndex == -1)
            {
                MessageBox.Show("Bạn chưa chọn nhân viên nào");
                return;
            }
            NhanVien nv = ModelManager.GetInstance().db.NhanViens.ToList().ElementAt(WindowsManager.GetInstance().TabNhanVien.SelectedIndex);
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa nhân viên: " + nv.MaNV + "?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ModelManager.GetInstance().db.NhanViens.Remove(nv);
                ModelManager.GetInstance().db.SaveChanges();
                LoadData();
            }
        }

        public override void OnHelpClick()
        {

        }

        public override void OnPrintClick()
        {

        }

        private void fAddNhanVien_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            LoadData();
        }

        public override TabType GetTabType()
        {
            return TabType.NhanVienTab;
        }

        public override void OnViewTDTDClick()
        {
            throw new NotImplementedException();
        }

        public override void OnViewTTTDClick()
        {
            throw new NotImplementedException();
        }

        public override void OnViewTSDBClick()
        {
            throw new NotImplementedException();
        }

        public override void OnEditTDTDClick()
        {
            throw new NotImplementedException();
        }

        public override void OnEditTTTDClick()
        {
            throw new NotImplementedException();
        }

        public override void OnAddTSDBClick()
        {
            throw new NotImplementedException();
        }

        public override void OnPrintTDTDClick()
        {
            throw new NotImplementedException();
        }

        public override void OnPrintTTTDClick()
        {
            throw new NotImplementedException();
        }

        public override void OnPrintTSDBClick()
        {
            throw new NotImplementedException();
        }

        public override void OnSearchKHClick()
        {
            throw new NotImplementedException();
        }
    }
}
